/***********************************************************************
 *            Project: CoreCms
 *        ProjectName: 核心内容管理系统                                
 *                Web: https://www.corecms.net                      
 *             Author: 大灰灰                                          
 *              Email: jianweie@163.com                                
 *         CreateTime: 2023/9/15 23:09:53
 *        Description: 暂无
 ***********************************************************************/


using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Filter;
using CoreCms.Net.Loging;
using CoreCms.Net.IServices;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.Utility.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using SqlSugar;
using CoreCms.Net.Services;
using CoreCms.Net.Web.Admin.Infrastructure;

namespace CoreCms.Net.Web.Admin.Controllers
{
    /// <summary>
    /// 微信发货快递公司信息
    ///</summary>
    [Description("微信发货快递公司信息")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    [RequiredErrorForAdmin]
    [Authorize(Permissions.Name)]
    public class WeChatShippingDeliveryController : ControllerBase
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly IWeChatShippingDeliveryServices _weChatShippingDeliveryServices;

        /// <summary>
        /// 构造函数
        ///</summary>
        public WeChatShippingDeliveryController(IWebHostEnvironment webHostEnvironment
            ,IWeChatShippingDeliveryServices weChatShippingDeliveryServices
            )
        {
            _webHostEnvironment = webHostEnvironment;
            _weChatShippingDeliveryServices = weChatShippingDeliveryServices;
        }

        #region 获取列表============================================================
        // POST: Api/WeChatShippingDelivery/GetPageList
         /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取列表")]
        public async Task<AdminUiCallBack> GetPageList()
        {
            var jm = new AdminUiCallBack();
            var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            var where = PredicateBuilder.True<WeChatShippingDelivery>();
            //获取排序字段
            var orderField = Request.Form["orderField"].FirstOrDefault();

            Expression<Func<WeChatShippingDelivery, object>> orderEx = orderField switch
            {
                "id" => p => p.id,"deliveryId" => p => p.deliveryId,"deliveryName" => p => p.deliveryName,
                _ => p => p.id
            };

            //设置排序方式
            var orderDirection = Request.Form["orderDirection"].FirstOrDefault();
            var orderBy = orderDirection switch
            {
                "asc" => OrderByType.Asc,
                "desc" => OrderByType.Desc,
                _ => OrderByType.Desc
            };
            //查询筛选
			
			//序列 int
			var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0);
            if (id > 0)
            {
                where = where.And(p => p.id == id);
            }
			//快递公司编码 nvarchar
			var deliveryId = Request.Form["deliveryId"].FirstOrDefault();
            if (!string.IsNullOrEmpty(deliveryId))
            {
                where = where.And(p => p.deliveryId.Contains(deliveryId));
            }
			//快递公司名称 nvarchar
			var deliveryName = Request.Form["deliveryName"].FirstOrDefault();
            if (!string.IsNullOrEmpty(deliveryName))
            {
                where = where.And(p => p.deliveryName.Contains(deliveryName));
            }
            //获取数据
            var list = await _weChatShippingDeliveryServices.QueryPageAsync(where, orderEx, orderBy, pageCurrent, pageSize, true);
            //返回数据
            jm.data = list;
            jm.code = 0;
            jm.count = list.TotalCount;
            jm.msg = "数据调用成功!";
            return jm;
        }
        #endregion

        #region 首页数据============================================================
        // POST: Api/WeChatShippingDelivery/GetIndex
        /// <summary>
        /// 首页数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("首页数据")]
        public AdminUiCallBack GetIndex()
        {
            //返回数据
            var jm = new AdminUiCallBack { code = 0 };
            return jm;
        }
        #endregion

        #region 拉取数据更新============================================================

        // POST: Api/CoreCmsLogistics/DoDelete/10
        /// <summary>
        ///     拉取数据更新
        /// </summary>
        [HttpPost]
        [Description("单选删除")]
        public async Task<AdminUiCallBack> DoUpdateCompany()
        {
            var jm = await _weChatShippingDeliveryServices.DoUpdateCompany();
            return jm;

        }

        #endregion

    }
}
